<?php

namespace App\Http\Controllers\admin;

use App\Dao\BaseDao;
use App\Dao\UsersDao;
use App\Dao\VipSettingsDao;
use App\Exceptions\SysExceptions;
use App\Model\UsersModel;
use App\Model\UsersPhoneLogModel;
use App\Services\Code;

/**
 * Created by PhpStorm.
 * @purpose 用户控制器
 * @Author: fhc
 * @Time: 2023/5/14 16:04
 */
class UsersController extends BaseController
{

    public function users(){
        if(getIsHasPage()){
            return Code::message('',200,UsersDao::get_user_page(request()->all()));
        }
        return view('admin.user.user');
    }


    /**
     * @throws SysExceptions
     */
    public function update_spread(){
        if(isPost()){
            UsersDao::update_spread(request()->all());
            return  Code::message('',200);
        }
        $userInfo= UsersDao::getUserInfo([
            'user_id'=>request('user_id')
        ]);
        $lists = VipSettingsDao::get_vips();
        return view('admin.user.update_spread',[
            'info'=>$userInfo,
            'list'=>$lists
        ]);
    }





    public function change_yong()
    {
        if(isPost()){
            $params = request()->all();
            UsersDao::change_yong($params);
            return Code::message();
        }
        return  view('admin.user.change_d_price');
    }


    public function get_user_phone_log(){
        if(getIsHasPage()){
            return  Code::message('',200,UsersPhoneLogModel::query()->orderByDesc('created_at')->paginate());
        }
        return view('admin.user.user_phone_log');
    }

    public function audit_user_phone_log(){
        if(isPost()){
            return  Code::message('',200,UsersDao::audit_user_phone(request()->all()));
        }

        return view('admin.user.audit_user_phone_log',[
            'info'=>UsersPhoneLogModel::query()->where('id',request('id'))->first()
        ]);
    }


    public function update_vip_level_index(){
        if(isPost()){
            if(UsersDao::get_user_builder([
                'user_id'=>request('user_id')
            ])->update([
                'vip_level_index'=>request('vip_level_index')
            ]) === false){
                BaseDao::error('修改失败');
            }
            return  Code::message();
        }

        return view('admin.user.update_vip_level_index',[
            'info'=>UsersModel::query()->where('user_id',request('user_id'))->first()
        ]);
    }
}
